(function ($) {
	
	/*
	 * return格式:{"a":"1","b":"2","c":"3"}
	 */
	$.fn.serializeObject = function()    
	{    
	   var o = {};    
	   var a = this.serializeArray();    
	   $.each(a, function() {    
	       if (o[this.name]) {    
	           if (!o[this.name].push) {    
	               o[this.name] = [o[this.name]];    
	           }    
	           o[this.name].push(this.value || '');    
	       } else {    
	           o[this.name] = this.value || '';    
	       }    
	   });    
	   return o;    
	};
	
	/*
	 * 入口
	 */
	$.fn.modalselar=function(options,databack,returnback){
		var pathName = document.location.pathname;
		var index = pathName.substr(1).indexOf("/");
		var path = pathName.substr(0, index + 1);
		
		var param=jQuery.extend({},options,{"databack":databack,"contextPath":path,"returnback":returnback});
		
		fn.creHtml(this,param);	
		fn.bindEvent(this,param);
		fn.creJqgrid(this,param);
		
		var $t=this;
		$(this).unbind("keydown")
		$(this).keydown(function(e){
			if(e.keyCode==13){
				if($("#modalselar_ipt_search").is(':focus')){
					$("#modalselar_btn-search").trigger("click");
				}else
					$("#modalselar_btn-sel").trigger("click");
			}
			if(e.keyCode==27){
				$("#modalselar_btn-colse").trigger("click");
			}
			//jqgrid上下选中行
			if (e.keyCode == 40){
				  if($("#modalselar_ipt_search").is(":focus")){
					  $t.focus();
					  $("#modalselar_ipt_search").blur();
				  } 
				  var grid_selector=$("#modalselar-grid-table");
				  var id=$(grid_selector).jqGrid("getGridParam","selrow");
				  $(grid_selector).jqGrid('resetSelection');
				  var newId;
				  var allId;
				  newId=parseInt(id)+1;
				  allId=$(grid_selector).jqGrid('getDataIDs');
				  if(newId>allId[allId.length-1])
					  newId=1
				  $(grid_selector).jqGrid('setSelection',newId);
				  e.preventDefault();
			}
			if(e.keyCode == 38){
				 if($("#modalselar_ipt_search").is(":focus")){
					 $("#modalselar_ipt_search").blur();
					 $t.focus();
				 }
					  
				 var grid_selector=$("#modalselar-grid-table");
				 var id=$(grid_selector).jqGrid("getGridParam","selrow");
				 $(grid_selector).jqGrid('resetSelection');
				 
				 var newId;
				 var allId;
				 newId=parseInt(id)-1;
				 var allId=$(grid_selector).jqGrid('getDataIDs');
					if(newId<1)
						newId=allId[allId.length-1];
				 $(grid_selector).jqGrid('setSelection',newId);
				 e.preventDefault();
			}
		});
		
		//读卡
		$t.find("#btnReadCard").click(function(){
			$("#modalselar_sel_type").val("");
			if(!$.isFunction($().oper_memberCard)){
				$.ajaxSetup({ async: false, cache: false });
	            $.getScript(path+"/static/hotel/frontoffice/js/plug-ins/selection/jquery.modal.oper_memberCard.js");
	            $.ajaxSetup({ async: true });
			}
			var keyarg = [{Action:"Read",Cardno:""}];
			var log_key = "[]";
			
			$("").oper_memberCard({
				keyarg : JSON.stringify(keyarg),
				log_key : log_key,
				dataBack : function(data){
//					console.log(JSON.stringify(data))
					var json_str = JSON.stringify(data).toLowerCase();
					var json = JSON.parse(json_str)
					$("#modalselar_ipt_search").val(json.cardno);
					$("#modalselar_ipt_search").trigger("change");
				}
			});
			
		})
	}
	
	/*
	 * 功能函数
	 */
	var fn={
		creHtml:function($t,param){
			var html="";
			html+='<div class="modal-dialog" style="width: 1000px;">'+
					'<div class="modal-content">'+
						'<div class="modal-header no-padding">'+
							'<div class="table-header">'+
								'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
									'<span class="white">&times;</span>'+
								'</button>'+
								'<span id="modal-h4">'+$.i18n.propJsp("请选择")+'&nbsp;'+'<i class="ace-icon fa fa-angle-double-right"></i>&nbsp;'+$.i18n.propJsp("储值卡")+'(AR)'+'</span>'+
							'</div>'+
						'</div>'+
						'<div class="modal-body">'+
							'<div class="row">'+
								'<div id="imgContainer" class="widget-box" style="display:none;z-index:10;width:602px;position:absolute;">'+
									'<div class="widget-header">'+
										'<div class="widget-toolbar">'+
											'<button id="photoHide" type="button" class="close" value="HTML">'+
												'<span class="red bigger-200">&times;</span>'+
											'</button>'+
										'</div>'+
									'</div>'+
									'<div class="widget-body">'+
										'<img src="" style="width:600px;"></img>'+
									'</div>'+
								'</div>'+
								'<div class="col-xs-8">'+
									'<div class="well well-sm">'+
										'<form id="modalselar_frm" class="form-horizontal" action="javacript:void(0);"  onsubmit="return false">'+
											'<div class="form-group">'+
												'<label class="col-sm-1 control-label">'+$.i18n.propJsp("组别")+'</label>'+
												'<div class="col-sm-3">'+
													'<select id="modalselar_sel_type" class="input-medium" name="dcode"></select>'+
												'</div>'+
												'<label class="col-sm-1 control-label">'+$.i18n.propJsp("卡号")+'</label>'+
												'<div class="col-sm-3">'+
													'<input id="modalselar_ipt_search" type="text" name="aname" class="input-medium" style="height:30px;"/>'+
												'</div>'+
												'<div class="col-sm-2">'+
													'<label style="margin-left: 5px;margin-top: 5px;">'+
				        								'<input data-disabled="disabled" id="chk_includegroup" name="chk_includegroup" type="checkbox" class="ace " value="0" onclick="this.value=(this.checked==true)?1:0"/> <span class="lbl" >'+$.i18n.propJsp("含集团")+'</span>'+
				        							'</label>'+
			        							'</div>'+
			        							'<div class="col-sm-2">'+
													'<button id="modalselar_btn-search" class="btn btn-sm btn-info no-padding" style="height:30px;">'+
														'<i class="ace-icon fa fa-search"></i>'+$.i18n.propJsp("查询")+
													'</button>'+
												'</div>'+
											'</div>'+
											'<div class="form-group">'+
												'<label class="col-sm-1 control-label">'+$.i18n.propJsp("名称")+'</label>'+
												'<div class="col-sm-3">'+
													'<input  type="text" name="text" class="input-medium" style="height:30px;"/>'+
												'</div>'+
												'<label class="col-sm-1 control-label">'+$.i18n.propJsp("电话")+'</label>'+
												'<div class="col-sm-3">'+
													'<input  type="text" name="phone" class="input-medium" style="height:30px;"/>'+
												'</div>'+
												'<div class="col-sm-2">'+
													'<label style="margin-left: 5px;margin-top: 5px;">'+
				        								'<input data-disabled="disabled" id="chk_onlyusable" name="chk_onlyusable" type="checkbox" class="ace " value="0" onclick="this.value=(this.checked==true)?1:0"/> <span class="lbl" >'+$.i18n.propJsp("只要可用")+'</span>'+
				        							'</label>'+
			        							'</div>'+
											'</div>'+
										'</form>'+
									'</div>'+
									'<div class="widget-box">'+
										'<table id="modalselar-grid-table"></table>'+
										'<div id="modalselar-grid-pager"></div>'+	
									'</div>'+
								'</div>'+
								'<div class="col-xs-4">'+
									'<div class="widget-box">'+
										'<div class="widget-body">'+
											'<div class="widget-main" style="height:470px;">'+
												'<div id="modalselarArInfo" class="sidebar-info">'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">GNO</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="gno"></span>'+  
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("组别")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="dcode_c"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">A/R'+$.i18n.propJsp("账号名称")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="text"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("账号")+'/'+$.i18n.propJsp("卡号")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="aname"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("电话")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="phone"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("可挂账金额")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="postlimit"></span>'+
														'</div>'+
													'</div>'+
//													'<div class="sidebar-info-row">'+
//														'<div class="sidebar-info-name">余额</div>'+
//														'<div class="sidebar-info-value">'+
//															'<span id="balance_c"></span>'+
//														'</div>'+
//													'</div>'+
//													'<div class="sidebar-info-row">'+
//														'<div class="sidebar-info-name">共享</div>'+
//														'<div class="sidebar-info-value">'+
//															'<span id="shareno"></span>'+
//														'</div>'+
//													'</div>'+
//													'<div class="sidebar-info-row">'+
//														'<div class="sidebar-info-name">签单人</div>'+
//														'<div class="sidebar-info-value">'+
//															'<span id="sname"></span>'+
//														'</div>'+
//													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("担保人")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="vname"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("总次数")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="time_s"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("天次数")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="day_s"></span>'+
														'</div>'+
													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("积分连接")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<span id="mar_mno"></span>'+
															'<span id="mar_mname"></span>'+
														'</div>'+
													'</div>'+
//													'<div class="sidebar-info-row">'+
//														'<div class="sidebar-info-name">积分卡名称</div>'+
//														'<div class="sidebar-info-value">'+
//															
//														'</div>'+
//													'</div>'+
													'<div class="sidebar-info-row">'+
														'<div class="sidebar-info-name">'+$.i18n.propJsp("签字留样")+'</div>'+
														'<div class="sidebar-info-value">'+
															'<img id="signaturebase64" src="" style="width:180px;"/>'+
														'</div>'+
													'</div>'+
												'</div>'+
											'</div>'+
											'<div class="widget-toolbox padding-8 clearfix" style="height:53px;">'+
												'<div class="pull-right" style="margin-top:4px;">'+
//													'<div class="btn-group hide">'+
//														'<button id="modalselar_btn-search">'+
//															'查询隐藏按钮'+
//														'</button>'+
//													'</div>'+
													'<div class="btn-group">'+
														'<button id="btnReadCard" class="btn btn-info btn-sm">'+
															'<i class="ace-icon fa fa-leanpub"></i>'+$.i18n.propJsp("读卡")+
														'</button>'+
													'</div>'+
													'<div class="btn-group">'+
														'<button class="btn btn-info btn-sm" id="modalselar_btn-sel">'+
															'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("选择")+
														'</button>'+
													'</div>'+
													'<div class="btn-group">'+
														'<button id="modalselar_btn-colse" class="btn btn-default btn-sm">'+
															'<i class="ace-icon fa fa-times"></i>'+$.i18n.propJsp("关闭")+
														'</button>'+
													'</div>'+
												'</div>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>';
						'</div>'+
					'</div>'+
			'</div>';	
			$($t).append(html);
			var select = $("select[name='dcode']")[0];
//			var json = sysgetdict(param.dict);
			var dict = $.parseJSON(localStorage.getItem("hodict"))
			var json = sysgetdictBySource(param.dict,dict)
			var all={"code":"","descript1":"全部"};
			json.splice(0, 0,all);
			sysjson2select(select, json);
			if(param.code!="")
				$(select).val(param.code);
			if(param.value!="")
				$("#modalselar_ipt_search").val(param.value);
			
			$("#chk_includegroup").trigger("click")
			$("#chk_onlyusable").trigger("click")
//			$("#modalselar_ipt_search").val(param.value==""?"":param.value);
			
		},
		bindEvent:function($t,param){
			
			var t = this;
			$($t).on('hidden.bs.modal', function () {
				$(".modal").css("opacity","1");
				param.returnback();
			 })
			
			//查询
			$("#modalselar_btn-search").bind("click",function(){
				fn.freJqgrid($t,param)
			});
			
			//条件表单change
			$($t).find("#modalselar_frm").change(function(){
				$($t).find("#modalselar_btn-search").trigger("click");
			})
			
			//条件表单回车
			$($t).find("#modalselar_frm").keyup(function(e){
				if(e.keyCode==13){
					$("#modalselar_btn-search").trigger("click");
				}
			})
			
			
			//组别变化
			$("#modalselar_sel_type").bind("change",function(){
				$("#modalselar_btn-search").trigger("click");
			});
			//关闭
			$("#modalselar_btn-colse,#close-dialog").bind("click",function(){
				param.returnback();
				$($t).modal("hide");
				$($t).empty();
				$(".modal").css("opacity","1");
			});
			
			
			//选择
			$("#modalselar_btn-sel").bind("click",function(){
				var id=$("#modalselar-grid-table").jqGrid('getGridParam','selrow');
				if(id==null)
					return;
				var rowData = $("#modalselar-grid-table").jqGrid("getRowData",id);
//				alert(JSON.stringify(rowData))
				if(rowData.password!=""){
					bootbox.prompt({
						title:$.i18n.propJsp("请输入密码") ,
					    inputType: 'password',
					    size: 'small',
						callback: function (result) {
							if(result==null){
								$(".bootbox").modal("hide");
								$(".bootbox").empty();
								$(".bootbox").remove();
								$(".modal-backdrop:last").remove();
								return;
							}
							if(result!=rowData.password){
								bootbox.alert({
								    message: '<h3 class="header smaller lighter red">'+$.i18n.propJsp("密码错误")+'</h3>',
								    size: 'small'
								});
							}else{
								$(".modal").css("opacity","1");
								$($t).empty();
								param.databack(rowData);
							}
						}
					});
				}else{
					$(".modal").css("opacity","1");
					$($t).empty();
					param.databack(rowData);
					$($t).modal("hide");
				}
			});
			
			$t.find("#signaturebase64").click(function(){

				var t = this;
				var src = $t.find("#signaturebase64").attr("src");
				var top = $t.find("#signaturebase64").position().top;
				var left = $t.find("#signaturebase64").position().left;
				$t.find("#imgContainer").find("img").eq(0).attr("src",src);
				$t.find("#imgContainer").css("top",200).css("left",100);
				$t.find("#imgContainer").show();
			});
			
			//点击放大隐藏
			$t.find("#photoHide").bind("click",function(){
				$t.find("#imgContainer").hide();
			})
			
		},
		initJs:function($t,param){
			var js={};
			js.wherestring=[];
			js.wherestring[0]="@@WS";
			js.wherestring[1]="";
			
			if($("#modalselar_sel_type").val()!="-1")
				js.wherestring[1]+=" dcode='"+$("#modalselar_sel_type").val()+"'";
			var item=$("#modalselar_ipt_search").val().trim();
			if(item!=""){
				js.wherestring[1]+=(js.wherestring[1]==""?" (aname Like '" + item + "%' ":" and (aname Like '" + item + "%' ");
				js.wherestring[1]+=" or text Like '" + item + "%' ";
				js.wherestring[1]+=" or dcode like '" + item + "%' ";
				js.wherestring[1]+=" or type = '" + item + "'";
				js.wherestring[1]+=" or sname like '%" + item + "%'"
				js.wherestring[1]+= " or aname Like '%" + item + "%' " + " or text Like '%" + item + 
						"%' or sname Like '%" + item + "%' or text1 Like '%" + item + 
						"%' or text2 Like '%" + item + "%' or phone Like '%" + item + "%') "
			}
			return js;
		},
		creJqgrid:function($t,param){
			var arg={};
			arg.ho_arcust={"aname":param.value,"dcode":$("#modalselar_sel_type").val(),
							text:param.value,phone:$t.find("input[name=phone]").val()};
			
			if(!$.isFunction($().jqGridFnModalSelAr)){
				$.ajaxSetup({ async: false, cache: false });
				$.getScript("js/cashier_mana/jquery.jqgrid.selar.js");
	            $.ajaxSetup({ async: true });
			}
//			var arg={};
//			arg.ho_arcust={"aname":param.value,"dcode":$("#modalselar_sel_type").val(),
//							text:param.value,phone:$t.find("input[name=phone]").val()};
			var frmJson = sysname2json($("#modalselar_frm").serializeArray());
			var arg = {ho_arcust:frmJson};
			var chk = {}
			chk.chk_includegroup = frmJson.chk_includegroup
			chk.chk_onlyusable = frmJson.chk_onlyusable
			arg.arg = chk

			$("#modalselar-grid-table").jqGridFnModalSelAr({
				oper:"jqGridCre",
				pager_selector:"#modalselar-grid-pager",    		
	    		height:"330",
	    		width:"644",
	    		postData:$.HSAPI_ASKJSONINFO(JSON.stringify(arg),'ListRetrieveHoArcustAndBoot','{}','listWithPage'),
	    		callBack:function(){
	    			$("#modalselar_btn-sel").trigger("click");
	    		},
	    		onSelectBack : function(rowdata){
	    			$("#modalselarArInfo").find("span").each(function(){
			    		$(this).text(rowdata[this.id]);
			    		$(this).attr("title",rowdata[this.id]);
			    	});
		        	$("#modalselarArInfo").find("#signaturebase64").attr("src",rowdata.signaturebase64);
	    		}
			});
		},
		freJqgrid:function($t,param){
			
//			var arg={};
//			arg.ho_arcust={"aname":$("#modalselar_ipt_search").val(),"dcode":$("#modalselar_sel_type").val(),
//					text:$t.find("input[name=text]").val(),phone:$t.find("input[name=phone]").val()};
			var frmJson = sysname2json($("#modalselar_frm").serializeArray());
			var arg = {ho_arcust:frmJson};
			var chk = {}
			chk.chk_includegroup = frmJson.chk_includegroup
			chk.chk_onlyusable = frmJson.chk_onlyusable
			arg.arg = chk
			$("#modalselar-grid-table").jqGridFnModalSelAr({
					oper:"jqGridFre",
					postData:$.HSAPI_ASKJSONINFO(JSON.stringify(arg),'ListRetrieveHoArcustAndBoot','{}','listWithPage'),
			});
		},
	}
	
})(jQuery);